/**
 * 
 */
package com.alpsoft.spotcheck.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;

import com.alpsot.lkspotcheck.R;

import static com.alpsot.lkspotcheck.R.drawable.*;

/**
 * 
 * @描述：图片工具类
 * @类名称：ImgUtil
 * @创建人：c.zhao
 * @创建时间：2015-5-21 下午3:34:28
 * 
 */
public class ImgUtil {
	
	/**
	 * 将drawable资源拷贝到sd卡
	 * @param context
	 * @param fileName
	 * @return
	 */
	@SuppressWarnings("ResourceType")
	public static String copyToSD(Context context, String fileName) {
		InputStream is = null;
		FileOutputStream fos = null;
		String path = GlobalVar.picPath + "/" + fileName;

		try {
			File file = new File(GlobalVar.picPath);
			if (!file.exists()){
				file.mkdir();
			}

			File sdFile = new File(path);
			if (!sdFile.exists()) {
				is = context.getResources().openRawResource(no_pic);
				fos = new FileOutputStream(sdFile);

				byte[] buffer = new byte[8 * 1024];// 8K
				while (is.read(buffer) > 0){
					fos.write(buffer);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}

				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

			}
		}
		return path;

	}

}
